Excel BI - Excel Challenge 767

excel-challenges
excel-formulas
🔰 Find the numbers with distinct digits between From & To (both inclusive) and give their counts, minimum number and maximum number.
Published

March 24, 2026

Illustration for Excel BI - Excel Challenge 767

Challenge Description

🔰 Find the numbers with distinct digits between From & To (both inclusive) and give their counts, minimum number and maximum number.

Solutions

library(tidyverse)
library(readxl)
library(gtools)

path = "Excel/700-799/767/767 Distinct Digits.xlsx"
input = read_excel(path, range = "A1:B6")
test  = read_excel(path, range = "C1:E6") %>% 
  mutate(across(everything(), as.integer))

distinct_digit_numbers = function(from, to) {
  rng = nchar(as.character(from)):nchar(as.character(to))
  pool = 0:9
  all_valid = map_dfr(rng, function(k) {
    map_dfr(setdiff(pool, 0), function(h) {
      tail_pool = setdiff(pool, h)
      tail_perms = permutations(length(tail_pool), k - 1, tail_pool)
      tibble(n = as.integer(paste0(h, apply(tail_perms, 1, paste0, collapse = ""))))
    })
  }) %>% filter(n >= from, n <= to)
  nvec = all_valid$n
  tibble(
    Count = length(nvec),
    Min = min(nvec, na.rm = TRUE),
    Max = max(nvec, na.rm = TRUE)
  )
}

result = map2_dfr(input$From, input$To, distinct_digit_numbers) 
all.equal(result, test) 
# > [1] TRUE
  • Logic: Read the workbook ranges needed for the challenge; Derive the required intermediate columns.
  • Strengths: The code maps the workbook rule into a compact, reproducible pipeline.
  • Areas for Improvement: The solution assumes the workbook layout and selected ranges remain stable, so any structural change in the sheet would require small adjustments.
  • Gem: The elegant part is how little code is needed once the correct intermediate representation is chosen.
from itertools import permutations
import pandas as pd

path = "700-799/767/767 Distinct Digits.xlsx"
input = pd.read_excel(path, usecols="A:B", nrows=6)
test = pd.read_excel(path, usecols="C:E", nrows=6)

def distinct_digit_perms(f, t):
    res = []
    for d in range(len(str(f)), len(str(t)) + 1):
        for p in permutations('0123456789', d):
            if p[0] != '0':
                n = int(''.join(p))
                if f <= n <= t:
                    res.append(n)
    return len(res), min(res), max(res)

input[['Count', 'Min', 'Max']] = pd.DataFrame(
    input.apply(lambda r: distinct_digit_perms(r['From'], r['To']), axis=1).tolist(), index=input.index
)
input.drop(columns=['From', 'To'], inplace=True)

print(input.equals(test))
# > True

The Python version keeps the algorithm explicit, which helps when the challenge depends on a greedy or iterative rule.

Difficulty Level

Easy / Medium

The business rule is clear, though the workbook still needs a few transformation steps to reach the expected output.